package com.superdry.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.util.AbCharacterParser;
import com.alibaba.fastjson.JSON;
import com.drysurper.R;
import com.superdry.adapter.CityListAdapter;
import com.superdry.json.Location;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.ToastHelper;

public class ChooseCityActivity extends Activity implements OnClickListener {

	private ListView city_list;
	private AbHttpUtil mAbHttpUtil = null;
	private ProgressDialog progressDialog;
	private CityListAdapter adapter;
	private EditText input_search_query;
	private LinearLayout choose_city_goback;
	Map<String, List<Location>> citys = new HashMap<String, List<Location>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.aty_choose_city);
		initView();
	}

	public void initView() {
		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(ChooseCityActivity.this);
		mAbHttpUtil.setTimeout(10000);
		city_list = (ListView) findViewById(R.id.city_list);
		input_search_query = (EditText) findViewById(R.id.input_search_query);
		choose_city_goback = (LinearLayout) findViewById(R.id.choose_city_goback);
		choose_city_goback.setOnClickListener(this);
		selectLocation();
		input_search_query.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable edit) {
				// 在这里处理搜索
				String search_city = input_search_query.getText().toString();
				String search_city_pinyin = AbCharacterParser.getInstance().getSelling(search_city);
				List<Location> search_result = new ArrayList<Location>();
				Iterator it = citys.keySet().iterator();
				
				while (it.hasNext()) {
					String key = it.next().toString();
					String key_pinyin = AbCharacterParser.getInstance().getSelling(key);
					if(key_pinyin.startsWith(search_city_pinyin)){
						search_result.addAll(search_result.size(), citys.get(key));
					}
				}
				adapter = new CityListAdapter(ChooseCityActivity.this, search_result);
				city_list.setAdapter(adapter);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.choose_city_goback:
			finish();
			break;

		default:
			break;
		}
	}

	/**
	 * 选择地区
	 */
	private void selectLocation() {

		AbRequestParams params = new AbRequestParams();
		mAbHttpUtil.post(GlobalConst.GET_ALL_LOCATION, params, new AbStringHttpResponseListener() {

			@Override
			public void onStart() {
				progressDialog = progressDialog.show(ChooseCityActivity.this, "数据加载中...", "请稍后 ...");
			}

			@Override
			public void onFinish() {
				progressDialog.dismiss();
			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				progressDialog.dismiss();
				ToastHelper.show(ChooseCityActivity.this, "获取地区信息失败");
			}

			@Override
			public void onSuccess(int statusCode, String content) {
				progressDialog.dismiss();
				List<Location> locations = JSON.parseArray(content, Location.class);
				for (Location location : locations) {
					addCityInMap(location.getLocationProvince(), location);
				}
				adapter = new CityListAdapter(ChooseCityActivity.this, locations);
				city_list.setAdapter(adapter);
			}
		});

	}

	/**
	 * 添加城市对应的地区
	 */
	public void addCityInMap(String key, Location value) {

		if (citys.containsKey(key)) {
			citys.get(key).add(value);
		} else {
			List<Location> strs = new ArrayList<Location>();
			strs.add(value);
			citys.put(key, strs);
		}
	}
}
